package cn.com.yx.ms.service.impl;

import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.PortalApplyInfo;
import cn.com.yx.ms.persistence.PortalApplyInfoMapper;
import cn.com.yx.ms.service.IPortalService;
import cn.yx.framework.configuration.ApiCode;
import com.google.code.kaptcha.Producer;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * Created by lenovo on 2017/5/26.
 */
@Service
@Transactional
public class PortalServiceImpl implements IPortalService {
    @Resource
    private PortalApplyInfoMapper portalApplyInfoMapper;
    @Resource
    private Producer captchaProducer = null;
    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public List<PortalApplyInfo> find(ApiQueryBase queryBase) {
        return portalApplyInfoMapper.find(queryBase);
    }

    @Override
    public ApiCode saveApplyInfo(PortalApplyInfo portalApplyInfo,String uuid) {
        ApiCode apiCode=ApiCode.APP_400501;
        String validCode=getValidCode(uuid);
        if (!validCode.equals(portalApplyInfo.getValidCode())){
            //验证码输入错误
            return ApiCode.APP_401503;
        }

        ApiQueryBase apiQueryBase=new ApiQueryBase();
        apiQueryBase.getConditions().put("mobile",portalApplyInfo.getMobile());
        apiQueryBase.getConditions().put("company",portalApplyInfo.getCompany());
        apiQueryBase.getConditions().put("state",portalApplyInfo.getState());
        List<PortalApplyInfo> portalApplyInfoList=portalApplyInfoMapper.find(apiQueryBase);
        if (null != portalApplyInfoList && portalApplyInfoList.size()>0){
            //已经存在该用户
            return apiCode=ApiCode.APP_401502;
        }
        int result=0;
        result= portalApplyInfoMapper.insertPortalApplyInfo(portalApplyInfo);
        return apiCode;
    }
    //获得验证码
    public String getValidCode(String redisKey) {
        String validCode = "";
        try {
            validCode = (String) redisTemplate.opsForValue().get(redisKey);
        }
        catch (Exception ex){
            System.out.println(ex.getMessage());
        }
        return validCode;
    }
    // endregion

    @Override
    public ApiCode updateByGuid(String guid) {
        ApiQueryBase apiQueryBase=new ApiQueryBase();
        apiQueryBase.getConditions().put("guid",guid);
        List<PortalApplyInfo> portalApplyInfoList=find(apiQueryBase);
        ApiCode apiCode=ApiCode.APP_401500;
        if(portalApplyInfoList.size()==0){
            return apiCode;
        }
        int result=0;
        result=portalApplyInfoMapper.updateByGuid(guid);
        if(result>0){
            return ApiCode.APP_400500;
        }else{
            return ApiCode.APP_401501;
        }
    }
}
